home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Development
/
General
/
PTR-TCL v2.1
/
4) header symbols
/
Munge.c
< prev
next >
Wrap
Text File
|
1994-02-09
|
8KB
|
379 lines
/*
* munge.c
*/
#include <string.h>
#include <TextUtils.h>
#define isalfa(x) ((((x)>='A')&&((x)<='Z'))||(((x)>='a')&&((x)<='z'))||((x)=='_')||(((x)>='0')&&((x)<='9')))
extern void Idle ( void ) ;
extern short vRefNum ;
void MungeFolder ( long parID ) ;
extern void Message ( unsigned char * ) ;
extern Boolean DoLine ( unsigned char * line ) ;
unsigned char global_token [ 100 ] ;
CInfoPBRec rec ;
short err ;
Str63 name ;
Str255 str ;
short lineNo ;
static void
CopyP ( unsigned char * from , unsigned char * to ) {
BlockMove ( from , to , 1 + * from ) ;
}
static void
ConcP ( unsigned char * to , unsigned char * tail ) {
short len = * tail ;
if ( len + * to > 255 ) {
len = 255 - * to ;
}
BlockMove ( tail + 1 , to + * to + 1 , len ) ;
* to += len ;
}
static void
ErrMsg ( unsigned char * why , unsigned char * file , short code ) {
Str15 nn ;
long tick ;
static long lastBeep = 0 ;
NumToString ( code , nn ) ;
CopyP ( "\p ERROR " , str ) ;
ConcP ( str , why ) ;
ConcP ( str , "\p " ) ;
ConcP ( str , nn ) ;
ConcP ( str , "\p\r" ) ;
Message ( str ) ;
tick = TickCount ( ) + 30 ;
if ( lastBeep < tick - 600 ) { /* Only beep every 10 seconds if lots of errors */
SysBeep ( 20 ) ;
lastBeep = tick ;
}
do {
Idle ( ) ;
} while ( TickCount ( ) < tick ) ;
}
static long
GetLine ( Handle text , long pos , unsigned char * into ) {
long end = GetHandleSize ( text ) ;
lineNo ++ ;
* into = 0 ;
while ( pos < end ) {
if ( * into == 255 ) {
ErrMsg ( "\pLong Line!" , "\pLine" , lineNo ) ;
return pos ;
}
( * into ) ++ ;
into [ * into ] = ( * text ) [ pos ] ;
pos ++ ;
if ( into [ * into ] == '\r' ) {
return pos ;
}
}
return pos ;
}
static long
PutLine ( Handle text , long pos , long len , unsigned char * str ) {
ConcP ( str , "\p// " ) ; /* Comment out old line */
Munger ( text , pos , NULL , 0L , str + 1 , * str ) ;
return pos + len + * str ;
}
static long
token ( Handle text , long pos , long end , char * tok ) {
char ch ;
Boolean f = 0 ;
while ( pos < end ) {
ch = ( * text ) [ pos ] ;
pos ++ ;
if ( isalfa ( ch ) ) {
f = 1 ;
* ( tok ++ ) = ch ;
} else {
if ( f ) {
* tok = 0 ;
if ( ch == '\r' ) {
pos -- ;
}
return pos ;
}
if ( ch == '\r' ) {
* tok = ch ;
tok [ 1 ] = 0 ;
return pos ;
}
}
}
return pos ;
}
/* This function contains the total text of a TEXT file. */
/* The contents of the handle will be re-written to disk when done if true returned. */
/* Call Idle ( ) now and then for lengthy tasks */
static Boolean
MungeHandle ( Handle text ) {
Handle hh ;
if ( 0 <= Munger ( text , 0 , & global_token [ 1 ] , global_token [ 0 ] , NULL , 0L ) ) {
return false ;
}
hh = NewHandle ( 0L ) ;
PtrAndHand ( "#ifndef " , hh , 8 ) ;
PtrAndHand ( & global_token [ 1 ] , hh , global_token [ 0 ] ) ;
PtrAndHand ( "\r# define " , hh , 10 ) ;
PtrAndHand ( & global_token [ 1 ] , hh , global_token [ 0 ] ) ;
PtrAndHand ( "\r\r" , hh , 2 ) ;
HandAndHand ( text , hh ) ;
PtrAndHand ( "\r#endif\r" , hh , 8 ) ;
SetHandleSize ( text , GetHandleSize ( hh ) ) ;
BlockMove ( * hh , * text , GetHandleSize ( hh ) ) ;
DisposeHandle ( hh ) ;
return true ;
}
#if 0
static Boolean
MungeHandle ( Handle text ) {
long end = GetHandleSize ( text ) ;
long pos = 0 ;
int level = 0 ;
char tok [ 256 ] ;
Boolean aGoto = 0 ;
Boolean aReturn = 0 ;
Boolean aGotoOK = 0 ;
Boolean aReturnOK = 0 ;
Boolean aNL = 0 ;
long line = 0 ;
Str255 ss ;
Str15 num ;
Boolean aDec = 0 ;
while ( pos < end ) {
aDec = 0 ;
pos = token ( text , pos , end , tok ) ;
if ( ! strcmp ( tok , "TRY" ) || ! strcmp ( tok , "TRY_MULTIPLE" ) ) {
level ++ ;
} else
if ( ! strcmp ( tok , "ENDTRY" ) || ! strcmp ( tok , "DONE" ) ) {
aNL = 1 ;
aDec = 1 ;
} else
if ( ! strcmp ( tok , "goto" ) ) {
aGoto = 1 ;
} else
if ( ! strcmp ( tok , "return" ) ) {
aReturn = 1 ;
} else
if ( ! strcmp ( tok , "\r" ) ) {
line ++ ;
aNL = 1 ;
} else
if ( ! strcmp ( tok , "GOTO_OK" ) ) {
aGotoOK = 1 ;
} else
if ( ! strcmp ( tok , "RETURN_OK" ) ) {
aReturnOK = 1 ;
}
if ( aGotoOK ) {
aGoto = 0 ;
}
if ( aReturnOK ) {
aReturn = 0 ;
}
if ( aNL ) {
if ( level ) {
if ( aReturn ) {
CopyP ( "\p RETURN error line " , ss ) ;
NumToString ( line , num ) ;
ConcP ( ss , num ) ;
Message ( ss );
}
if ( aGoto ) {
CopyP ( "\p GOTO error line " , ss ) ;
NumToString ( line , num ) ;
ConcP ( ss , num ) ;
Message ( ss );
}
}
if ( aDec ) {
level -- ;
}
if ( level < 0 ) {
CopyP ( "\p level error line " , ss ) ;
NumToString ( line , num ) ;
ConcP ( ss , num ) ;
Message ( ss );
}
aNL = 0 ;
aGoto = 0 ;
aReturn = 0 ;
aGotoOK = 0 ;
aReturnOK = 0 ;
if ( ! ( line % 10 ) ) {
Idle ( ) ;
}
}
}
if ( level ) {
Message ( "\p END level error" ) ;
}
return false ;
}
#endif
static void
MungeFile ( short vRef , long parID , unsigned char * fn ) {
Handle text ;
short ref = 0 ;
long size ;
Boolean doWrite ;
int pos = 0 ;
unsigned char * pp ;
int cnt ;
Message ( fn ) ;
if ( fn [ * fn ] != 'h' ) {
return ;
}
global_token [ ++ pos ] = '_' ;
global_token [ ++ pos ] = '_' ;
pp = fn + 1 ;
cnt = * fn ;
while ( cnt > 0 ) {
cnt -- ;
if ( ( * pp >= 'a' && * pp <= 'z' ) || ( *pp >= 'A' && *pp <= 'Z' ) ) {
global_token [ ++ pos ] = * ( pp ++ ) ;
} else {
if ( * pp != '.' ) {
global_token [ ++ pos ] = '_' ;
pp ++ ;
} else {
break ; // Don't want .h
}
}
}
global_token [ ++ pos ] = '_' ;
global_token [ ++ pos ] = '_' ;
global_token [ 0 ] = pos ;
UpperText ( ( Ptr ) & global_token [ 1 ] , global_token [ 0 ] ) ;
err = HOpenDF ( vRef , parID , fn , fsRdWrPerm , & ref ) ;
if ( err ) {
ErrMsg ( "\pOpening" , fn , err ) ;
return ;
}
err = GetEOF ( ref , & size ) ;
if ( ! err ) {
err = SetFPos ( ref , fsFromStart , 0L ) ;
}
if ( err ) {
ErrMsg ( "\pStarting" , fn , err ) ;
FSClose ( ref ) ;
return ;
}
text = NewHandle ( size ) ;
if ( ! text ) {
ErrMsg ( "\pAllocating" , fn , MemError ( ) ) ;
FSClose ( ref ) ;
return ;
}
HLock ( text ) ;
err = FSRead ( ref , & size , * text ) ;
if ( err ) {
ErrMsg ( "\pReading" , fn , err ) ;
FSClose ( ref ) ;
DisposeHandle ( text ) ;
}
HUnlock ( text ) ;
doWrite = MungeHandle ( text ) ;
if ( doWrite ) {
size = GetHandleSize ( text ) ;
err = SetEOF ( ref , size ) ;
if ( ! err ) {
err = SetFPos ( ref , fsFromStart , 0L ) ;
}
if ( err ) {
ErrMsg ( "\pFinishing" , fn , err ) ;
FSClose ( ref ) ;
DisposeHandle ( text ) ;
return ;
}
HLock ( text ) ;
err = FSWrite ( ref , & size , * text ) ;
if ( err ) {
ErrMsg ( "\pWriting" , fn , err ) ;
FSClose ( ref ) ;
DisposeHandle ( text ) ;
return ;
}
}
err = FSClose ( ref ) ;
if ( doWrite && ! err ) {
err = FlushVol ( NULL , vRef ) ;
}
DisposeHandle ( text ) ;
if ( err ) {
ErrMsg ( "\pClosing" , fn , err ) ;
}
Message ( "\p DONE\r" ) ;
}
void
MungeFolder ( long parID ) {
short ix ;
for ( ix = 1 ; ix > 0 ; ix ++ ) {
rec . hFileInfo . ioCompletion = 0L ;
rec . hFileInfo . ioFVersNum = 0 ;
rec . hFileInfo . ioNamePtr = name ;
rec . hFileInfo . ioVRefNum = vRefNum ;
rec . hFileInfo . ioDirID = parID ;
rec . hFileInfo . ioFDirIndex = ix ;
err = PBGetCatInfoAsync ( & rec ) ;
do {
Idle ( ) ;
} while ( rec . hFileInfo . ioResult == 1 ) ;
err = rec . hFileInfo . ioResult ;
if ( err ) {
break ;
}
if ( rec . hFileInfo . ioFlAttrib & 0x10 ) { /* Folder */
CopyP ( "\p\r•Entering Folder " , str ) ;
ConcP ( str , name ) ;
ConcP ( str , "\p\r" ) ;
Message ( str ) ;
MungeFolder ( rec . hFileInfo . ioDirID ) ;
Message ( "\p---- Leaving folder ----\r\r" ) ;
} else if ( rec . hFileInfo . ioFlFndrInfo . fdType == 'TEXT' ) {
MungeFile ( vRefNum , parID , name ) ;
}
}
}